// packageI/ecosphere_watercooler/owner/home/home.js

const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    apply_flag:0,
    text:"",
    Height: 0,
    LastHeight: 0,
    merchant: {},
    distribution_url:"",
    carousel: [],

    distribution_name:"",
    distribution_url:"",
    business_name:"",
    business_url:"",
    customer_url:"",
    customer_name:"",
    external_name:"",
    external_url:"",
    external_appid:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getData();
    this.get_audit();
  },
  get_audit(){
    app._getNetWork({
      url: app.getNetAddresss('plugin.water-machine.frontend.business.merchant-audit.index'),
      success: resdata => {
        let {
          data,
          msg,
          result
        } = resdata.data;
        if (result) {
          if(data.status==1){
            this.setData({
              apply_flag: 1,
              text: '机主信息'
            })
          }else{
            this.setData({
              apply_flag: 0,
              text: '机主申请'
            })

          }

        } else {
          app.tips(msg)
        }
      }
    })
  },
  link(e){
    let url = e.currentTarget.dataset.mini_url;
    if(url){
      wx.navigateTo({
        url,
      })

    }
  },
  imgHeight: function (e) {
    var winWid = wx.getSystemInfoSync().windowWidth; //获取当前屏幕的宽度
    var imgh = e.detail.height; //图片高度
    var imgw = e.detail.width; //图片宽度
    var swiperH = winWid * imgh / imgw + "px"; //等比设置swiper的高度。 即 屏幕宽度 / swiper高度 = 图片宽度 / 图片高度  ==》swiper高度 = 屏幕宽度 * 图片高度 / 图片宽度
    if (parseInt(swiperH) > this.data.LastHeight) {
      this.setData({
        Height: swiperH, //设置高度
      });
      this.data.LastHeight = parseInt(swiperH);
    }
  },
  getData() {
    app._getNetWork({
      url: app.getNetAddresss('plugin.water-machine.frontend.business.merchant.index'),
      success: resdata => {
        let {
          data,
          msg,
          result
        } = resdata.data;
        if (result) {
          this.setData({
            distribution_url:data.distribution_url,
            merchant: data.merchant,
            carousel: data.carousel,
            distribution_name:data.distribution_name,
            distribution_url:data.distribution_url,
            business_name:data.business_name,
            business_url:data.business_url,
            customer_url:data.customer_url,
            customer_name:data.customer_name,
            external_name:data.external_name,
            external_url:data.external_url,
            external_appid:data.external_appid,
          })
        } else {
          app.tips(msg)
        }
      }
    })

  },
  goto(e) {
    let url = e.currentTarget.dataset.url;
    if (url == 'customer') {
      wx.navigateTo({
        url: '/packageF/others/customerCenter/customerCenterIndex/customerCenterIndex',
      })
    } else if (url == 'withdrawal') {
      wx.navigateTo({
        url: '/packageA/member/withdrawal/withdrawal',
      })
    } else if (url == 'extension') {
      if(this.data.distribution_url){
        wx.navigateTo({
          url: this.data.distribution_url,
        })
      }
    }else if(url=='apply_for'){
      wx.navigateTo({
        url: `/packageI/ecosphere_watercooler/owner/${e.currentTarget.dataset.url}/${e.currentTarget.dataset.url}?apply_flag=${this.data.apply_flag}`
      })
    }else if(url=="distribution_url"||url=="customer_url"||url=='business_url'){
      wx.navigateTo({
        url:this.data[url]
      })
    }else if(url=="external_url"){
      wx.navigateToMiniProgram({
        appId:this.data.external_appid,
        path:this.data.external_url,
        success(res) {
          // 打开成功
          
        },
        fail() {
          wx.showToast({
            title: '小程序跳转失败',
            icon: 'none',
            duration: 2000
          });
        }
      }) 
    }else {
      wx.navigateTo({
        url: `/packageI/ecosphere_watercooler/owner/${e.currentTarget.dataset.url}/${e.currentTarget.dataset.url}`
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})